package com.itheima.wisdomnext.service.ImpI;

import com.itheima.wisdomnext.Exception.BusinessException;
import com.itheima.wisdomnext.entity.Favorite;
import com.itheima.wisdomnext.entity.Result.enumcalss.ErrorCode;
import com.itheima.wisdomnext.mapper.FavoriteMapper;
import com.itheima.wisdomnext.service.FavoriteService;
import com.itheima.wisdomnext.util.FavoriteS_Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    public void addFavorite(Favorite favorite) {
        favoriteMapper.insertFavorite(favorite);
    }

    @Override
    public List<Favorite> getFavoritesByUserId(Integer userId) {
        List<Favorite> favoritesByUserId = favoriteMapper.findFavoritesByUserId(userId);
        if (favoritesByUserId == null || favoritesByUserId.isEmpty()) {
            FavoriteS_Util.isTrue(true, ErrorCode.FAILED, "获取某个用户的所有收藏记录失败");
        }
        return favoritesByUserId;
    }

    @Override
    public void deleteFavorite(Favorite favorite) {
        favoriteMapper.deleteFavorite(favorite);
    }

    // 检查用户是否收藏该帖子
    @Override
    public int isPostFavorited(Integer userId, Integer targetId) {
        return favoriteMapper.isPostFavorited(userId, targetId);
    }
}
